package Leetcode;

import base.ListNode;

/**
 * 给你单链表的头节点 head ，请你反转链表，并返回反转后的链表。
 */
public class 力扣206_反转链表 {

    public static void main(String[] args) {
        ListNode a1 = new ListNode(1);
        ListNode a2 = new ListNode(2);
        ListNode a3 = new ListNode(3);
        ListNode a4 = new ListNode(4);
        ListNode a5 = new ListNode(5);
        a1.next = a2;
        a2.next = a3;
        a3.next = a4;
        a4.next = a5;

        ListNode listNode = reverseList(a1);
        System.out.println(listNode);
    }

    public static ListNode reverseList(ListNode head) {
        ListNode pre = null;
        ListNode cur = null;

        while (head != null) {
            cur = head;
            ListNode next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }

        return cur;
    }
}
